{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 前$k$个常用单词\n",
    "\n",
    "![Asked by AirBNB](https://img.shields.io/badge/Asked%20by-AirBNB-important?style=social&logo=airbnb)\n",
    "\n",
    "&emsp;&emsp;给定一个非空的单词列表，返回$k$个最常用的单词。\n",
    "\n",
    "&emsp;&emsp;**输出应以频率降序为第一索引序、字典顺序为第二索引序**。输入仅包含小写字母。\n",
    "\n",
    "```text\n",
    " In[1]: [\"daily\", \"interview\", \"pro\", \"pro\", \"for\", \"daily\", \"pro\", \"problems\"]\n",
    "        k = 2\n",
    "\n",
    "Out[1]: [\"pro\", \"daily\"]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "    def topFrequentK(self, words, k):\n",
    "        counter = Counter(words)\n",
    "        counter = counter.most_common(k)\n",
    "        return [item[0] for item in counter]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['pro', 'daily']\n"
     ]
    }
   ],
   "source": [
    "print(Solution().topFrequentK(\n",
    "    [\"daily\", \"interview\", \"pro\", \"pro\", \"for\", \"daily\", \"pro\", \"problems\"],\n",
    "    2\n",
    "))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
